﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title>Artistic Style C# Explicit</title>
    <meta http-equiv="Content-Language" content="en-us" />
    <meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
    <link href="../favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <link href="styles.css" rel="stylesheet" type="text/css" />
    <link href="andes.css" rel="stylesheet" type="text/css" />
</head>

<body>

    <h1>Artistic Style Developer Information</h1>

    <h2 class="large">
        Calling Artistic Style from a C# Program<br />
        Using Explicit Linking
    </h2>

    <p>
        &nbsp;</p>

    <p class="noindent">
        Artistic Style can be compiled as a shared library (DLL) and called from a C# program.</p>
    <p class="noindent">
        A C# program compiled with &quot;Any CPU&quot; can be run on any platform. Artistic Style, however, is a C++ program
        and must be compiled for the platform on which it will run. Once this is done Artistic Style can be called from
        a C# program.</p>
    <p class="noindent">
        With explicit linking the shared library name can be changed at runtime, allowing the C# program to load 
        either an x86 or x64 bit shared library. This provides the capability to make the program run on &quot;Any 
        CPU&quot;. The shared object can also be loaded from any directory, not just the directories searched by the 
        system.</p>

    <h3>Compile Options</h3>

    <p>
        To compile Artistic Style for use with a C# program the compile option ASTYLE_LIB must be defined. Then it will
        accept the files and options as parameters from a function call instead of the command line. It is the responsibility
        of the calling program to read the source files and accept the options from the user via a graphical interface
        or other method. These are then passed via the function call described below. After the source files are formatted
        they will be returned to the calling program, which must then save the source file and take other appropriate
        action.</p>

    <h3>Explicit Linking</h3>

    <p>
        Explicit linking can be used in C# with C language DLLs. The procedure is similar to using explicit linking in
        a C or C++ program with the additional task of marshaling objects between the languages.</p>
    <p>
        With explicit linking, applications must make function calls to explicitly load the DLL at run time. To explicitly
        link to a DLL, an application must use the following Windows functions, or their Linux equivalent:</p>
    <ul>
        <li>LoadLibrary (or a similar function) to load the DLL and obtain a module handle.</li>
        <li>GetProcAddress to obtain a function pointer to each exported function that the application wants to call. Because
            applications are calling the DLL&#8217;s functions through a pointer, the compiler does not generate external
            references, so there is no need to link with an import library or the DLL itself.</li>
        <li>Call FreeLibrary when done with the DLL.</li>
    </ul>
    <p>
        The AStyleInterface class using explicit linking has internal classes to define the DLL functions for Windows
        and Linux in addition to the AStyleInterface class. The class constructor handles the LoadLibrary
        and GetProcAddress function calls while the destructor handles the FreeLibrary function. The LoadLibrary and FreeLibrary
        functions in the DLL have a reference count and will load and free the library only when needed. Static methods
        are NOT required, but may be used if you want.</p>
    <p>
        Explicit linking in the following AStyleInterface example was based on 
        <a href="http://dimitry-i.blogspot.com/2013/01/mononet-how-to-dynamically-load-native.html"
            target="_blank" title="open new window">C#: How to dynamically load native libraries; cross-platform 
            edition</a>.</p>

    <h3>AStyleMainUtf16 Method</h3>

    <p>
        This method is called to format the source code. Wide strings ARE used here.</p>

    <h4>Syntax</h4>

    <div class="hl">
        <pre class="hl">    <span class="hl slc">/// AStyleMainUtf16 function call.</span>
    <span class="hl slc">/// NOTE: Wide strings ARE used here (UTF-16 in BOTH Windows and Linux).</span>
    <span class="hl kwa">delegate</span> IntPtr <span class="hl kwd">AStyleMainUtf16</span><span class="hl opt">([</span><span class="hl kwd">MarshalAs</span><span class="hl opt">(</span>UnmanagedType<span class="hl opt">.</span>LPWStr<span class="hl opt">)]</span> <span class="hl kwb">string</span> textIn<span class="hl opt">,</span>
                                    <span class="hl opt">[</span><span class="hl kwd">MarshalAs</span><span class="hl opt">(</span>UnmanagedType<span class="hl opt">.</span>LPWStr<span class="hl opt">)]</span> <span class="hl kwb">string</span> options<span class="hl opt">,</span>
                                    AStyleErrorDelgate AStyleError<span class="hl opt">,</span>
                                    AStyleMemAllocDelgate AStyleMemAlloc<span class="hl opt">);</span>
</pre>
    </div>

    <h4>Parameters</h4>

    <p>
        <i>textIn</i><br />
        A <strong>wide-string</strong> pointer to the source file to be formatted.</p>
    <p>
        <i>options</i><br />
        A <strong>wide-string</strong> pointer to the formatting options. They should be in the same format as in the
        default options file. The options may be set apart by new-lines, commas, tabs or spaces. The long options do not
        need the "--" prefix. Comments may be used, but they must be terminated by a new-line "\n" character.</p>
    <p>
        If the file is not a C/C++ file, the file mode option "mode=java" or "mode=cs" must be included. Otherwise the
        default mode of C/C++ is used.</p>
    <p>
        <i>AStyleError</i><br />
        A function pointer to the error handling function. If there are errors in the parameters textIn or options, this
        function is called. See the following section on Callback Methods for more information..</p>
    <p>
        <i>AStyleMemAlloc</i><br />
        A function pointer to the memory allocation function. The calling program must allocate memory
        for the output source file. See the following section on Callback Methods for more information.</p>
    <p>
        The calling program is responsible for freeing the allocated memory when it is no longer needed.</p>

    <h4>Return Value</h4>

    <p>
        If the function succeeds, the return value is an IntPtr to the formatted source code. This function returns a
        16-bit Unicode string. The IntPtr should be converted to a C# string using Marshal.PtrToStringUni.</p>
    <p>
        If the function fails, the return value is a null pointer. Before the pointer is returned, an error message
        will be sent to the error handling function.</p>
    <p>
        This function typically fails for one of the following reasons:</p>
    <ul>
        <li>an invalid parameter value, usually an invalid pointer.</li>
        <li>the memory allocation function (memAllocFunc) returns a NULL.</li>
    </ul>
    <p>
        The function will NOT fail for an invalid option in the formatting options. In this case an error message 
        is sent to the error handling function and the formatted source code is returned without using the invalid 
        option.</p>

    <h4>Remarks</h4>

    <p>
        The calling program is responsible for freeing the memory allocated by memAllocFunc when it is no longer 
        needed.</p>
    <p>
        AStyleMainUtf16 should be called using a function pointer. The function pointer is obtained by a call to 
        GetProcAddress and marshaled to a function pointer using Marshal.GetDelegateForFunctionPointer. See the 
        AStyleInterface constructor in the example for the procedure.</p>

    <h3>Callback Methods</h3>

    <p>
        Callback methods must be delegated for the call to AStyleMainUtf16. Wide strings are NOT used here.</p>

    <h4>Syntax</h4>

    <div class="hl">
        <pre class="hl">    <span class="hl slc">/// AStyleError callback in AStyle.</span>
    <span class="hl slc">/// NOTE: Wide strings are NOT used here.</span>
    <span class="hl kwa">delegate</span> <span class="hl kwb">void</span> <span class="hl kwd">AStyleErrorDelgate</span><span class="hl opt">(</span><span class="hl kwb">int</span> errorNumber<span class="hl opt">,</span>
                                     <span class="hl opt">[</span><span class="hl kwd">MarshalAs</span><span class="hl opt">(</span>UnmanagedType<span class="hl opt">.</span>LPStr<span class="hl opt">)]</span> <span class="hl kwb">string</span> error<span class="hl opt">);</span>

    <span class="hl slc">/// AStyleMemAlloc callback in AStyle.</span>
    <span class="hl kwa">delegate</span> IntPtr <span class="hl kwd">AStyleMemAllocDelgate</span><span class="hl opt">(</span><span class="hl kwb">int</span> size<span class="hl opt">);</span>
</pre>
    </div>


    <h4>Remarks</h4>

    <p>
        The delegates AStyleErrorDelegate and AStyleMemAllocDelegate 
        delegate the error handling&nbsp; and memory allocation methods. These
        methods&nbsp; process any errors and allocate the required memory returned from AStyleMainUtf16. The delegates
        are similar to C++ function pointers, but are type safe.</p>
    <p>
        <i>AStyleErrorDelgate</i><br />
        AStyleError is called if there are errors in the string parameters in the call to AStyleMainUtf16. It should
        display an error message and then either abort or continue the program depending on the error. The first parameter
        is a number identifying the error. The second parameter is a pointer to a standard error message. The error message
        is Not Unicode. The message is deleted when the error handler returns.</p>
    <p>
        Error messages numbered 100-199 are errors that prevent the file from being formatted. A NULL pointer is returned
        to the calling program. Error messages numbered 200-299 are errors that do NOT prevent the file from being formatted.
        A valid string containing a formatted file is returned. This will occur if an invalid option is sent to AStyleMainUtf16.
        The calling program has the option of accepting or rejecting the formatted file.</p>
    <p>
        <i>AStyleMemAllocDelgate</i><br />
        AStyleMemAlloc is the memory allocation method. The program must allocate
        memory for the output source file. This function will be called when the memory is needed. The parameter is the
        amount of memory that should be allocated
        in a number of char's.</p>
    <p>
        Memory should be allocated using Marshal.AllocHGlobal. With this method, if there is an allocation exception,
        a NULL pointer will be sent to AStyleMainUtf16. Artistic Style will generate an error message and return an empty
        string from the AStyleMainUtf16 function. The calling program can handle the exception at this point rather than
        in the memory allocation function. See the following example program for the procedure.</p>
    <p>
        The calling program is responsible for freeing the memory allocated by AStyleMemAlloc when it is no longer 
        needed.</p>

    <h3>AStyleGetVersion Method</h3>

    <p>
        This method is called to get the Artistic Style version number.</p>

    <h4>Syntax</h4>

    <div class="hl">
        <pre class="hl">    <span class="hl slc">/// AStyleGetVersion function call.</span>
    <span class="hl slc">/// NOTE: Wide strings are NOT returned here.</span>
    <span class="hl kwa">delegate</span> IntPtr <span class="hl kwd">AStyleGetVersion</span><span class="hl opt">();</span>
</pre>
    </div>

    <h4>Return Value</h4>

    <p>
        The return value is an IntPtr to the Artistic Style version number. This function does NOT return Unicode. The
        IntPtr should be converted to a C# string using Marshal.PtrToStringAnsi.</p>

    <h4>Remarks</h4>

    <p>
        AStyleGetVersion should be called using a function pointer. The function pointer is obtained by a 
        call to GetProcAddress and marshaled to a function pointer using Marshal.GetDelegateForFunctionPointer. See 
        the AStyleInterface constructor in the example for the procedure.</p>

    <h3>Example</h3>

    <p>
        The following example formats source files by calling the Artistic Style formatter. It is a console application,
        but the procedure for a GUI is the same. The two classes can be copied and pasted into source files. Or they can
        be downloaded with test data from the "Developer Information" page. </p>
    <p>
        The Artistic Style source code must be compiled
        as a shared library (DLL) using the option ASTYLE_LIB. The shared library must be copied to the directory that
        contains the C# executable (it is loaded from the current directory). The directory of the files to be formatted
        is an absolute path in the function GetProjectDirectory() in Example.cs. This will need to be changed to reflect
        your directory structure. When the program is run be sure the current working directory is the directory with
        the executable and shared library.</p>
    <p>
        The Example class is identical in both C# examples. The differences are in the AStyleInterface class.</p>
    <p>
        &nbsp;</p>

    <div class="hl">
        <pre class="hl">    
<span class="hl slc">// Example.cs</span>

<span class="hl kwa">using</span> System<span class="hl opt">;</span>
<span class="hl kwa">using</span> System<span class="hl opt">.</span>IO<span class="hl opt">;</span>
<span class="hl kwa">using</span> System<span class="hl opt">.</span>Text<span class="hl opt">;</span>

<span class="hl slc">/// Example opens the source files, calls the AStyleInterface methods</span>
<span class="hl slc">/// to format the files, and saves the reformatted source. The files</span>
<span class="hl slc">/// are in a test-data directory. The option mode=cs must be included</span>
<span class="hl slc">/// for C# files.</span>
<span class="hl kwa">public class</span> Example
<span class="hl opt">{</span>   <span class="hl slc">/// Main function for this example.</span>
    <span class="hl kwa">public static void</span> <span class="hl kwd">Main</span><span class="hl opt">(</span><span class="hl kwa">string</span><span class="hl opt">[]</span> args<span class="hl opt">)</span>
    <span class="hl opt">{</span>   <span class="hl slc">// files to pass to AStyle</span>
        <span class="hl kwa">string</span><span class="hl opt">[]</span> fileName <span class="hl opt">=  {</span> <span class="hl str">&quot;ASBeautifier.cpp&quot;</span><span class="hl opt">,</span>
                               <span class="hl str">&quot;ASFormatter.cpp&quot;</span><span class="hl opt">,</span>
                               <span class="hl str">&quot;astyle.h&quot;</span>
                             <span class="hl opt">};</span>

        <span class="hl slc">// options to pass to AStyle</span>
        <span class="hl slc">// mode=cs is required for C# files</span>
        <span class="hl kwa">string</span> options <span class="hl opt">=</span> <span class="hl str">&quot;-A2tOP&quot;</span><span class="hl opt">;</span>

        <span class="hl slc">// create an object</span>
        AStyleInterface AStyle <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">AStyleInterface</span><span class="hl opt">();</span>

        <span class="hl slc">// get Artistic Style version</span>
        <span class="hl slc">// does not need to terminate on an error</span>
        <span class="hl kwa">string</span> version <span class="hl opt">=</span> AStyle<span class="hl opt">.</span><span class="hl kwd">GetVersion</span><span class="hl opt">();</span>
        <span class="hl kwa">if</span> <span class="hl opt">(</span>version <span class="hl opt">!=</span> String<span class="hl opt">.</span>Empty<span class="hl opt">)</span>
            Console<span class="hl opt">.</span><span class="hl kwd">WriteLine</span><span class="hl opt">(</span><span class="hl str">&quot;Example C# - AStyle &quot;</span> <span class="hl opt">+</span> version<span class="hl opt">);</span>

        <span class="hl slc">// process the files</span>
        <span class="hl kwa">for</span> <span class="hl opt">(</span><span class="hl kwa">int</span> i <span class="hl opt">=</span> <span class="hl num">0</span><span class="hl opt">;</span> i <span class="hl opt">&lt;</span> fileName<span class="hl opt">.</span>Length<span class="hl opt">;</span> i<span class="hl opt">++)</span>
        <span class="hl opt">{</span>   <span class="hl slc">// get the text to format</span>
            <span class="hl kwa">string</span> filePath <span class="hl opt">=</span> <span class="hl kwd">GetTestDirectoryPath</span><span class="hl opt">() +</span> fileName<span class="hl opt">[</span>i<span class="hl opt">];</span>
            <span class="hl kwa">string</span> textIn <span class="hl opt">=</span> <span class="hl kwd">GetText</span><span class="hl opt">(</span>filePath<span class="hl opt">);</span>

            <span class="hl slc">// call the Artistic Style formatting function</span>
            <span class="hl slc">// does not need to terminate on an error</span>
            <span class="hl kwa">string</span> textOut <span class="hl opt">=</span> AStyle<span class="hl opt">.</span><span class="hl kwd">FormatSource</span><span class="hl opt">(</span>textIn<span class="hl opt">,</span> options<span class="hl opt">);</span>
            <span class="hl slc">// does not need to terminate on an error</span>
            <span class="hl slc">// an error message has been displayed by the error handler</span>
            <span class="hl kwa">if</span> <span class="hl opt">(</span>textOut <span class="hl opt">==</span> String<span class="hl opt">.</span>Empty<span class="hl opt">)</span>
            <span class="hl opt">{</span>   Console<span class="hl opt">.</span><span class="hl kwd">WriteLine</span><span class="hl opt">(</span><span class="hl str">&quot;Cannot format &quot;</span>  <span class="hl opt">+</span> filePath<span class="hl opt">);</span>
                <span class="hl kwa">continue</span><span class="hl opt">;</span>
            <span class="hl opt">}</span>

            <span class="hl slc">// return the formatted text</span>
            Console<span class="hl opt">.</span><span class="hl kwd">WriteLine</span><span class="hl opt">(</span><span class="hl str">&quot;Formatted &quot;</span> <span class="hl opt">+</span> fileName<span class="hl opt">[</span>i<span class="hl opt">]);</span>
            <span class="hl kwd">SetText</span><span class="hl opt">(</span>textOut<span class="hl opt">,</span> filePath<span class="hl opt">);</span>
        <span class="hl opt">}</span>

        <span class="hl kwa">return</span><span class="hl opt">;</span>
    <span class="hl opt">}</span>

    <span class="hl slc">///  Error message function for this example.</span>
    <span class="hl kwa">private static void</span> <span class="hl kwd">Error</span><span class="hl opt">(</span><span class="hl kwa">string</span> message<span class="hl opt">)</span>
    <span class="hl opt">{</span>   Console<span class="hl opt">.</span><span class="hl kwd">WriteLine</span><span class="hl opt">(</span>message<span class="hl opt">);</span>
        Console<span class="hl opt">.</span><span class="hl kwd">WriteLine</span><span class="hl opt">(</span><span class="hl str">&quot;The program has terminated!&quot;</span><span class="hl opt">);</span>
        Environment<span class="hl opt">.</span><span class="hl kwd">Exit</span><span class="hl opt">(</span><span class="hl num">1</span><span class="hl opt">);</span>
    <span class="hl opt">}</span>

    <span class="hl slc">/// Find the test directory path from the application program path.</span>
    <span class="hl slc">/// This may need to be changed for your directory structure.</span>
    <span class="hl kwa">private static string</span> <span class="hl kwd">GetTestDirectoryPath</span><span class="hl opt">()</span>
    <span class="hl opt">{</span>   <span class="hl kwa">string</span> topDirectory <span class="hl opt">=</span> <span class="hl str">&quot;astyledev&quot;</span><span class="hl opt">;</span>
        <span class="hl kwa">string</span> appDirectory <span class="hl opt">=</span> System<span class="hl opt">.</span>AppDomain<span class="hl opt">.</span>CurrentDomain<span class="hl opt">.</span>BaseDirectory<span class="hl opt">;</span>
        <span class="hl kwa">if</span> <span class="hl opt">(</span>String<span class="hl opt">.</span><span class="hl kwd">IsNullOrEmpty</span><span class="hl opt">(</span>appDirectory<span class="hl opt">))</span>
            <span class="hl kwd">Error</span><span class="hl opt">(</span><span class="hl str">&quot;Cannot find application directory!&quot;</span><span class="hl opt">);</span>
        <span class="hl kwa">int</span> indexTop <span class="hl opt">=</span> appDirectory<span class="hl opt">.</span><span class="hl kwd">ToLower</span><span class="hl opt">().</span><span class="hl kwd">IndexOf</span><span class="hl opt">(</span>topDirectory<span class="hl opt">);</span>
        <span class="hl kwa">if</span> <span class="hl opt">(</span>indexTop <span class="hl opt">== -</span><span class="hl num">1</span><span class="hl opt">)</span>
            <span class="hl kwd">Error</span><span class="hl opt">(</span><span class="hl str">&quot;Cannot find top level folder!&quot;</span><span class="hl opt">);</span>
        <span class="hl kwa">string</span> testPath <span class="hl opt">=</span> appDirectory<span class="hl opt">.</span><span class="hl kwd">Substring</span><span class="hl opt">(</span>
                              <span class="hl num">0</span><span class="hl opt">,</span> indexTop <span class="hl opt">+</span> topDirectory<span class="hl opt">.</span>Length<span class="hl opt">) +</span> <span class="hl str">&quot;/test-data/&quot;</span><span class="hl opt">;</span>
        <span class="hl kwa">return</span> testPath<span class="hl opt">;</span>
    <span class="hl opt">}</span>

    <span class="hl slc">///  Get the text to be formatted.</span>
    <span class="hl slc">///  Usually the text would be obtained from an edit control.</span>
    <span class="hl kwa">private static string</span> <span class="hl kwd">GetText</span><span class="hl opt">(</span><span class="hl kwa">string</span> filePath<span class="hl opt">)</span>
    <span class="hl opt">{</span>   <span class="hl slc">// create input buffers</span>
        <span class="hl kwa">const int</span> readSize <span class="hl opt">=</span> <span class="hl num">131072</span><span class="hl opt">;</span>     <span class="hl slc">// 128 KB</span>
        StringBuilder bufferIn <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">StringBuilder</span><span class="hl opt">(</span>readSize<span class="hl opt">);</span>
        <span class="hl kwa">char</span><span class="hl opt">[]</span> fileIn <span class="hl opt">=</span> <span class="hl kwa">new char</span><span class="hl opt">[</span>readSize<span class="hl opt">];</span>

        <span class="hl slc">// read file data</span>
        <span class="hl kwa">try</span>
        <span class="hl opt">{</span>   FileStream file <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">FileStream</span><span class="hl opt">(</span>filePath<span class="hl opt">,</span> FileMode<span class="hl opt">.</span>Open<span class="hl opt">);</span>
            StreamReader streamIn <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">StreamReader</span><span class="hl opt">(</span>file<span class="hl opt">);</span>
            <span class="hl slc">// use ReadBlock to preserve the current line endings</span>
            <span class="hl kwa">int</span> charsIn <span class="hl opt">=</span> streamIn<span class="hl opt">.</span><span class="hl kwd">ReadBlock</span><span class="hl opt">(</span>fileIn<span class="hl opt">,</span> <span class="hl num">0</span><span class="hl opt">,</span> readSize<span class="hl opt">);</span>
            <span class="hl kwa">while</span> <span class="hl opt">(</span>charsIn <span class="hl opt">!=</span> <span class="hl num">0</span><span class="hl opt">)</span>
            <span class="hl opt">{</span>   bufferIn<span class="hl opt">.</span><span class="hl kwd">Append</span><span class="hl opt">(</span>fileIn<span class="hl opt">,</span> <span class="hl num">0</span><span class="hl opt">,</span> charsIn<span class="hl opt">);</span>
                charsIn <span class="hl opt">=</span> streamIn<span class="hl opt">.</span><span class="hl kwd">ReadBlock</span><span class="hl opt">(</span>fileIn<span class="hl opt">,</span> <span class="hl num">0</span><span class="hl opt">,</span> readSize<span class="hl opt">);</span>
            <span class="hl opt">}</span>
            streamIn<span class="hl opt">.</span><span class="hl kwd">Close</span><span class="hl opt">();</span>
        <span class="hl opt">}</span>
        <span class="hl kwa">catch</span> <span class="hl opt">(</span>DirectoryNotFoundException e<span class="hl opt">)</span>
        <span class="hl opt">{</span>   Console<span class="hl opt">.</span><span class="hl kwd">WriteLine</span><span class="hl opt">(</span>e<span class="hl opt">.</span><span class="hl kwd">ToString</span><span class="hl opt">());</span>
            <span class="hl kwd">Error</span><span class="hl opt">(</span><span class="hl str">&quot;Cannot find directory &quot;</span> <span class="hl opt">+</span> filePath<span class="hl opt">);</span>
        <span class="hl opt">}</span>
        <span class="hl kwa">catch</span> <span class="hl opt">(</span>FileNotFoundException e<span class="hl opt">)</span>
        <span class="hl opt">{</span>   Console<span class="hl opt">.</span><span class="hl kwd">WriteLine</span><span class="hl opt">(</span>e<span class="hl opt">.</span><span class="hl kwd">ToString</span><span class="hl opt">());</span>
            <span class="hl kwd">Error</span><span class="hl opt">(</span><span class="hl str">&quot;Cannot find file &quot;</span> <span class="hl opt">+</span> filePath<span class="hl opt">);</span>
        <span class="hl opt">}</span>
        <span class="hl kwa">catch</span> <span class="hl opt">(</span>Exception e<span class="hl opt">)</span>
        <span class="hl opt">{</span>   Console<span class="hl opt">.</span><span class="hl kwd">WriteLine</span><span class="hl opt">(</span>e<span class="hl opt">.</span><span class="hl kwd">ToString</span><span class="hl opt">());</span>
            <span class="hl kwd">Error</span><span class="hl opt">(</span><span class="hl str">&quot;Error reading file &quot;</span> <span class="hl opt">+</span> filePath<span class="hl opt">);</span>
        <span class="hl opt">}</span>

        <span class="hl kwa">return</span> bufferIn<span class="hl opt">.</span><span class="hl kwd">ToString</span><span class="hl opt">();</span>
    <span class="hl opt">}</span>

    <span class="hl slc">///  Return the formatted text.</span>
    <span class="hl slc">///  Usually the text would be returned to an edit control.</span>
    <span class="hl kwa">private static void</span> <span class="hl kwd">SetText</span><span class="hl opt">(</span><span class="hl kwa">string</span> textOut<span class="hl opt">,</span> <span class="hl kwa">string</span> filePath<span class="hl opt">)</span>
    <span class="hl opt">{</span>   <span class="hl slc">// create a backup file</span>
        <span class="hl kwa">string</span> origfilePath <span class="hl opt">=</span> filePath <span class="hl opt">+</span> <span class="hl str">&quot;.orig&quot;</span><span class="hl opt">;</span>
        File<span class="hl opt">.</span><span class="hl kwd">Delete</span><span class="hl opt">(</span>origfilePath<span class="hl opt">);</span>                  <span class="hl slc">// remove a pre-existing file</span>
        FileInfo outFile <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">FileInfo</span><span class="hl opt">(</span>filePath<span class="hl opt">);</span>
        outFile<span class="hl opt">.</span><span class="hl kwd">MoveTo</span><span class="hl opt">(</span>origfilePath<span class="hl opt">);</span>

        <span class="hl slc">// write the output file - same name as input</span>
        <span class="hl kwa">try</span>
        <span class="hl opt">{</span>   <span class="hl kwa">char</span><span class="hl opt">[]</span> bufferOut <span class="hl opt">=</span> textOut<span class="hl opt">.</span><span class="hl kwd">ToCharArray</span><span class="hl opt">();</span>
            FileStream file <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">FileStream</span><span class="hl opt">(</span>filePath<span class="hl opt">,</span> FileMode<span class="hl opt">.</span>Create<span class="hl opt">);</span>
            StreamWriter streamOut <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">StreamWriter</span><span class="hl opt">(</span>file<span class="hl opt">);</span>
            streamOut<span class="hl opt">.</span><span class="hl kwd">Write</span><span class="hl opt">(</span>bufferOut<span class="hl opt">,</span> <span class="hl num">0</span><span class="hl opt">,</span> bufferOut<span class="hl opt">.</span>Length<span class="hl opt">);</span>
            streamOut<span class="hl opt">.</span><span class="hl kwd">Close</span><span class="hl opt">();</span>
        <span class="hl opt">}</span>
        <span class="hl kwa">catch</span> <span class="hl opt">(</span>Exception e<span class="hl opt">)</span>
        <span class="hl opt">{</span>   Console<span class="hl opt">.</span><span class="hl kwd">WriteLine</span><span class="hl opt">(</span>e<span class="hl opt">.</span><span class="hl kwd">ToString</span><span class="hl opt">());</span>
            <span class="hl kwd">Error</span><span class="hl opt">(</span><span class="hl str">&quot;Error writing file &quot;</span> <span class="hl opt">+</span> filePath<span class="hl opt">);</span>
        <span class="hl opt">}</span>

        <span class="hl kwa">return</span><span class="hl opt">;</span>
    <span class="hl opt">}</span>

<span class="hl opt">}</span>   <span class="hl slc">// class Example</span>

</pre>
    </div>
    <p>
        &nbsp;</p>
    <p>
        Beginning with version 2.04 of Artistic Style, UTF-16 Unicode can be used by calling the function AStyleMainUtf16.
        With C#, UTF-16 Unicode is used in both Windows and Linux.</p>
    <p>
        &nbsp;</p>
    <div class="hl">
        <pre class="hl"> 
<span class="hl slc">// AStyleInterface.cs</span>

<span class="hl kwa">using</span> System<span class="hl opt">;</span>
<span class="hl kwa">using</span> System<span class="hl opt">.</span>IO<span class="hl opt">;</span>
<span class="hl kwa">using</span> System<span class="hl opt">.</span>Runtime<span class="hl opt">.</span>InteropServices<span class="hl opt">;</span>

<span class="hl kwa">interface</span> NativeLibrary
<span class="hl opt">{</span>   <span class="hl kwa">void</span> <span class="hl kwd">FreeLibrary</span><span class="hl opt">(</span>IntPtr handle<span class="hl opt">);</span>
    IntPtr <span class="hl kwd">GetProcAddress</span><span class="hl opt">(</span>IntPtr handle<span class="hl opt">,</span> <span class="hl kwa">string</span> procName<span class="hl opt">);</span>
    IntPtr <span class="hl kwd">LoadLibrary</span><span class="hl opt">(</span><span class="hl kwa">string</span> fileName<span class="hl opt">);</span>
<span class="hl opt">}</span>

<span class="hl slc">/// Windows native methods.</span>
<span class="hl kwa">internal class</span> NativeLibraryWindows <span class="hl opt">:</span> NativeLibrary
<span class="hl opt">{</span>   <span class="hl kwa">void</span> NativeLibrary<span class="hl opt">.</span><span class="hl kwd">FreeLibrary</span><span class="hl opt">(</span>IntPtr handle<span class="hl opt">)</span>
    <span class="hl opt">{</span>   <span class="hl kwd">FreeLibrary</span><span class="hl opt">(</span>handle<span class="hl opt">);</span>
    <span class="hl opt">}</span>

    IntPtr NativeLibrary<span class="hl opt">.</span><span class="hl kwd">GetProcAddress</span><span class="hl opt">(</span>IntPtr handle<span class="hl opt">,</span> <span class="hl kwa">string</span> procName<span class="hl opt">)</span>
    <span class="hl opt">{</span>   <span class="hl kwa">return</span> <span class="hl kwd">GetProcAddress</span><span class="hl opt">(</span>handle<span class="hl opt">,</span> procName<span class="hl opt">);</span>
    <span class="hl opt">}</span>

    IntPtr NativeLibrary<span class="hl opt">.</span><span class="hl kwd">LoadLibrary</span><span class="hl opt">(</span><span class="hl kwa">string</span> fileName<span class="hl opt">)</span>
    <span class="hl opt">{</span>   <span class="hl kwa">return</span> <span class="hl kwd">LoadLibrary</span><span class="hl opt">(</span>fileName<span class="hl opt">);</span>
    <span class="hl opt">}</span>

    <span class="hl opt">[</span><span class="hl kwd">DllImport</span><span class="hl opt">(</span><span class="hl str">&quot;kernel32.dll&quot;</span><span class="hl opt">)]</span>
    <span class="hl kwa">private static extern</span> IntPtr <span class="hl kwd">LoadLibrary</span><span class="hl opt">(</span><span class="hl kwa">string</span> fileName<span class="hl opt">);</span>

    <span class="hl opt">[</span><span class="hl kwd">DllImport</span><span class="hl opt">(</span><span class="hl str">&quot;kernel32.dll&quot;</span><span class="hl opt">)]</span>
    <span class="hl kwa">private static extern int</span> <span class="hl kwd">FreeLibrary</span><span class="hl opt">(</span>IntPtr handle<span class="hl opt">);</span>

    <span class="hl opt">[</span><span class="hl kwd">DllImport</span><span class="hl opt">(</span><span class="hl str">&quot;kernel32.dll&quot;</span><span class="hl opt">)]</span>
    <span class="hl kwa">private static extern</span> IntPtr <span class="hl kwd">GetProcAddress</span><span class="hl opt">(</span>IntPtr handle<span class="hl opt">,</span> <span class="hl kwa">string</span> procName<span class="hl opt">);</span>
<span class="hl opt">}</span>


<span class="hl slc">/// Linux native methods.</span>
<span class="hl kwa">internal class</span> NativeLibraryLinux <span class="hl opt">:</span> NativeLibrary
<span class="hl opt">{</span>   <span class="hl kwa">public void</span> <span class="hl kwd">FreeLibrary</span><span class="hl opt">(</span>IntPtr handle<span class="hl opt">)</span>
    <span class="hl opt">{</span>   <span class="hl kwd">dlclose</span><span class="hl opt">(</span>handle<span class="hl opt">);</span>
    <span class="hl opt">}</span>

    <span class="hl kwa">public</span> IntPtr <span class="hl kwd">GetProcAddress</span><span class="hl opt">(</span>IntPtr handle<span class="hl opt">,</span> <span class="hl kwa">string</span> procName<span class="hl opt">)</span>
    <span class="hl opt">{</span>   <span class="hl kwd">dlerror</span><span class="hl opt">();</span>      <span class="hl slc">// clear previous errors if any</span>
        <span class="hl kwa">var</span> res <span class="hl opt">=</span> <span class="hl kwd">dlsym</span><span class="hl opt">(</span>handle<span class="hl opt">,</span> procName<span class="hl opt">);</span>
        <span class="hl kwa">var</span> errPtr <span class="hl opt">=</span> <span class="hl kwd">dlerror</span><span class="hl opt">();</span>
        <span class="hl kwa">if</span> <span class="hl opt">(</span>errPtr <span class="hl opt">!=</span> IntPtr<span class="hl opt">.</span>Zero<span class="hl opt">)</span>
            Console<span class="hl opt">.</span><span class="hl kwd">WriteLine</span><span class="hl opt">(</span><span class="hl str">&quot;dlsym: &quot;</span> <span class="hl opt">+</span> Marshal<span class="hl opt">.</span><span class="hl kwd">PtrToStringAnsi</span><span class="hl opt">(</span>errPtr<span class="hl opt">));</span>
        <span class="hl kwa">return</span> res<span class="hl opt">;</span>
    <span class="hl opt">}</span>

    <span class="hl kwa">public</span> IntPtr <span class="hl kwd">LoadLibrary</span><span class="hl opt">(</span><span class="hl kwa">string</span> fileName<span class="hl opt">)</span>
    <span class="hl opt">{</span>   <span class="hl kwd">dlerror</span><span class="hl opt">();</span>      <span class="hl slc">// clear previous errors if any</span>
        <span class="hl kwa">var</span> res <span class="hl opt">=</span> <span class="hl kwd">dlopen</span><span class="hl opt">(</span>fileName<span class="hl opt">,</span> RTLD_NOW<span class="hl opt">);</span>
        <span class="hl kwa">var</span> errPtr <span class="hl opt">=</span> <span class="hl kwd">dlerror</span><span class="hl opt">();</span>
        <span class="hl kwa">if</span> <span class="hl opt">(</span>errPtr <span class="hl opt">!=</span> IntPtr<span class="hl opt">.</span>Zero<span class="hl opt">)</span>
            Console<span class="hl opt">.</span><span class="hl kwd">WriteLine</span><span class="hl opt">(</span><span class="hl str">&quot;dlopen: &quot;</span> <span class="hl opt">+</span> Marshal<span class="hl opt">.</span><span class="hl kwd">PtrToStringAnsi</span><span class="hl opt">(</span>errPtr<span class="hl opt">));</span>
        <span class="hl kwa">return</span> res<span class="hl opt">;</span>
    <span class="hl opt">}</span>

    <span class="hl kwa">const int</span> RTLD_NOW <span class="hl opt">=</span> <span class="hl num">2</span><span class="hl opt">;</span>

    <span class="hl opt">[</span><span class="hl kwd">DllImport</span><span class="hl opt">(</span><span class="hl str">&quot;libdl&quot;</span><span class="hl opt">)]</span>
    <span class="hl kwa">private static extern</span> IntPtr <span class="hl kwd">dlopen</span><span class="hl opt">(</span><span class="hl kwa">string</span> fileName<span class="hl opt">,</span> <span class="hl kwa">int</span> flags<span class="hl opt">);</span>

    <span class="hl opt">[</span><span class="hl kwd">DllImport</span><span class="hl opt">(</span><span class="hl str">&quot;libdl&quot;</span><span class="hl opt">)]</span>
    <span class="hl kwa">private static extern</span> IntPtr <span class="hl kwd">dlsym</span><span class="hl opt">(</span>IntPtr handle<span class="hl opt">,</span> <span class="hl kwa">string</span> symbol<span class="hl opt">);</span>

    <span class="hl opt">[</span><span class="hl kwd">DllImport</span><span class="hl opt">(</span><span class="hl str">&quot;libdl&quot;</span><span class="hl opt">)]</span>
    <span class="hl kwa">private static extern int</span> <span class="hl kwd">dlclose</span><span class="hl opt">(</span>IntPtr handle<span class="hl opt">);</span>

    <span class="hl opt">[</span><span class="hl kwd">DllImport</span><span class="hl opt">(</span><span class="hl str">&quot;libdl&quot;</span><span class="hl opt">)]</span>
    <span class="hl kwa">private static extern</span> IntPtr <span class="hl kwd">dlerror</span><span class="hl opt">();</span>
<span class="hl opt">}</span>

<span class="hl slc">/// AStyleInterface contains methods to load the C shared libraries</span>
<span class="hl slc">/// and call the Artistic Style formatter.</span>
<span class="hl kwa">public class</span> AStyleInterface
<span class="hl opt">{</span>   <span class="hl slc">/// AStyleGetVersion function call.</span>
    <span class="hl slc">/// NOTE: Wide strings are NOT returned here.</span>
    <span class="hl kwa">delegate</span> IntPtr <span class="hl kwd">AStyleGetVersion</span><span class="hl opt">();</span>

    <span class="hl slc">/// AStyleError callback in AStyle.</span>
    <span class="hl slc">/// NOTE: Wide strings are NOT used here.</span>
    <span class="hl kwa">delegate void</span> <span class="hl kwd">AStyleErrorDelgate</span><span class="hl opt">(</span><span class="hl kwa">int</span> errorNumber<span class="hl opt">,</span>
                                     <span class="hl opt">[</span><span class="hl kwd">MarshalAs</span><span class="hl opt">(</span>UnmanagedType<span class="hl opt">.</span>LPStr<span class="hl opt">)]</span> <span class="hl kwa">string</span> error<span class="hl opt">);</span>

    <span class="hl slc">/// AStyleMemAlloc callback in AStyle.</span>
    <span class="hl kwa">delegate</span> IntPtr <span class="hl kwd">AStyleMemAllocDelgate</span><span class="hl opt">(</span><span class="hl kwa">int</span> size<span class="hl opt">);</span>

    <span class="hl slc">/// AStyleMainUtf16 function call.</span>
    <span class="hl slc">/// NOTE: Wide strings ARE used here (UTF-16 in BOTH Windows and Linux).</span>
    <span class="hl kwa">delegate</span> IntPtr <span class="hl kwd">AStyleMainUtf16</span><span class="hl opt">([</span><span class="hl kwd">MarshalAs</span><span class="hl opt">(</span>UnmanagedType<span class="hl opt">.</span>LPWStr<span class="hl opt">)]</span> <span class="hl kwa">string</span> textIn<span class="hl opt">,</span>
                                    <span class="hl opt">[</span><span class="hl kwd">MarshalAs</span><span class="hl opt">(</span>UnmanagedType<span class="hl opt">.</span>LPWStr<span class="hl opt">)]</span> <span class="hl kwa">string</span> options<span class="hl opt">,</span>
                                    AStyleErrorDelgate AStyleError<span class="hl opt">,</span>
                                    AStyleMemAllocDelgate AStyleMemAlloc<span class="hl opt">);</span>

    <span class="hl kwa">private</span> NativeLibrary astyle <span class="hl opt">=</span> <span class="hl kwa">null</span><span class="hl opt">;</span>

    <span class="hl kwa">private</span> IntPtr astyleHandle <span class="hl opt">=</span> IntPtr<span class="hl opt">.</span>Zero<span class="hl opt">;</span>

    <span class="hl kwa">private</span> AStyleMainUtf16 fpAStyleMainUtf16 <span class="hl opt">=</span> <span class="hl kwa">null</span><span class="hl opt">;</span>

    <span class="hl kwa">private</span> AStyleGetVersion fpAStyleGetVersion <span class="hl opt">=</span> <span class="hl kwa">null</span><span class="hl opt">;</span>

    <span class="hl slc">/// The constructor will load the native Artistic Style library and</span>
    <span class="hl slc">/// get the procedure addresses to call functions in the library.</span>
    <span class="hl slc">/// LoadLibrary has a reference count and will load the library only when needed.</span>
    <span class="hl kwa">public</span> <span class="hl kwd">AStyleInterface</span><span class="hl opt">()</span>
    <span class="hl opt">{</span>   astyle <span class="hl opt">=</span> <span class="hl kwd">IsWindows</span><span class="hl opt">()</span>
                 <span class="hl opt">? (</span>NativeLibrary<span class="hl opt">)</span><span class="hl kwa">new</span> <span class="hl kwd">NativeLibraryWindows</span><span class="hl opt">()</span>
                 <span class="hl opt">:</span> <span class="hl kwa">new</span> <span class="hl kwd">NativeLibraryLinux</span><span class="hl opt">();</span>

        <span class="hl slc">// must indicate the current directory for the Linux shared object</span>
        <span class="hl kwa">string</span> libraryName <span class="hl opt">=</span> <span class="hl str">&quot;./&quot;</span> <span class="hl opt">+</span> <span class="hl kwd">GetLibraryName</span><span class="hl opt">();</span>
        <span class="hl slc">// load the library</span>
        <span class="hl slc">// loadLibrary has a reference count and will load the library only when needed</span>
        <span class="hl kwa">if</span> <span class="hl opt">(!</span>File<span class="hl opt">.</span><span class="hl kwd">Exists</span><span class="hl opt">(</span>libraryName<span class="hl opt">))</span>
        <span class="hl opt">{</span>   <span class="hl kwd">Error</span><span class="hl opt">(</span><span class="hl str">&quot;Cannot find native library &quot;</span> <span class="hl opt">+</span> libraryName<span class="hl opt">);</span>
        <span class="hl opt">}</span>
        astyleHandle <span class="hl opt">=</span> astyle<span class="hl opt">.</span><span class="hl kwd">LoadLibrary</span><span class="hl opt">(</span>libraryName<span class="hl opt">);</span>
        <span class="hl kwa">if</span> <span class="hl opt">(</span>astyleHandle <span class="hl opt">==</span> IntPtr<span class="hl opt">.</span>Zero<span class="hl opt">)</span>
        <span class="hl opt">{</span>   Console<span class="hl opt">.</span><span class="hl kwd">WriteLine</span><span class="hl opt">(</span><span class="hl str">&quot;Cannot load the native library &quot;</span> <span class="hl opt">+</span> libraryName<span class="hl opt">);</span>
            <span class="hl kwd">Error</span><span class="hl opt">(</span><span class="hl str">&quot;You may be mixing 32 and 64 bit code!&quot;</span><span class="hl opt">);</span>
        <span class="hl opt">}</span>

        <span class="hl slc">// get the procedure address for AStyleGetVersion</span>
        <span class="hl kwa">var</span> astyleVersionHandle <span class="hl opt">=</span> astyle<span class="hl opt">.</span><span class="hl kwd">GetProcAddress</span><span class="hl opt">(</span>astyleHandle<span class="hl opt">,</span> <span class="hl str">&quot;AStyleGetVersion&quot;</span><span class="hl opt">);</span>
        <span class="hl kwa">if</span> <span class="hl opt">(</span>astyleVersionHandle <span class="hl opt">==</span> IntPtr<span class="hl opt">.</span>Zero<span class="hl opt">)</span>
        <span class="hl opt">{</span>   Console<span class="hl opt">.</span><span class="hl kwd">WriteLine</span><span class="hl opt">(</span><span class="hl str">&quot;Cannot find the symbol AStyleGetVersion in &quot;</span> <span class="hl opt">+</span> libraryName<span class="hl opt">);</span>
            <span class="hl kwd">Error</span><span class="hl opt">(</span><span class="hl str">&quot;The function must be undecorated in the library.&quot;</span><span class="hl opt">);</span>
        <span class="hl opt">}</span>
        fpAStyleGetVersion <span class="hl opt">= (</span>AStyleGetVersion<span class="hl opt">)</span>Marshal<span class="hl opt">.</span><span class="hl kwd">GetDelegateForFunctionPointer</span><span class="hl opt">(</span>
                                 astyleVersionHandle<span class="hl opt">,</span> <span class="hl kwa">typeof</span><span class="hl opt">(</span>AStyleGetVersion<span class="hl opt">));</span>

        <span class="hl slc">// get the procedure address for AStyleMainUtf16</span>
        <span class="hl kwa">var</span> astyleMainUtf16Handle <span class="hl opt">=</span> astyle<span class="hl opt">.</span><span class="hl kwd">GetProcAddress</span><span class="hl opt">(</span>astyleHandle<span class="hl opt">,</span> <span class="hl str">&quot;AStyleMainUtf16&quot;</span><span class="hl opt">);</span>
        <span class="hl kwa">if</span> <span class="hl opt">(</span>astyleMainUtf16Handle <span class="hl opt">==</span> IntPtr<span class="hl opt">.</span>Zero<span class="hl opt">)</span>
        <span class="hl opt">{</span>   Console<span class="hl opt">.</span><span class="hl kwd">WriteLine</span><span class="hl opt">(</span><span class="hl str">&quot;Cannot find the symbol AStyleMainUtf16 in &quot;</span> <span class="hl opt">+</span> libraryName<span class="hl opt">);</span>
            <span class="hl kwd">Error</span><span class="hl opt">(</span><span class="hl str">&quot;The function must be undecorated in the library.&quot;</span><span class="hl opt">);</span>
        <span class="hl opt">}</span>
        fpAStyleMainUtf16 <span class="hl opt">= (</span>AStyleMainUtf16<span class="hl opt">)</span>Marshal<span class="hl opt">.</span><span class="hl kwd">GetDelegateForFunctionPointer</span><span class="hl opt">(</span>
                                astyleMainUtf16Handle<span class="hl opt">,</span> <span class="hl kwa">typeof</span><span class="hl opt">(</span>AStyleMainUtf16<span class="hl opt">));</span>
    <span class="hl opt">}</span>

    <span class="hl slc">/// The destructor will free the native Artistic Style library.</span>
    <span class="hl slc">/// FreeLibrary has a reference count and will free the library only when needed.</span>
    ~<span class="hl kwd">AStyleInterface</span><span class="hl opt">()</span>
    <span class="hl opt">{</span>   astyle<span class="hl opt">.</span><span class="hl kwd">FreeLibrary</span><span class="hl opt">(</span>astyleHandle<span class="hl opt">);</span>
    <span class="hl opt">}</span>

    <span class="hl slc">/// Error handler to abort the program.</span>
    <span class="hl kwa">private void</span> <span class="hl kwd">Error</span><span class="hl opt">(</span><span class="hl kwa">string</span> message<span class="hl opt">)</span>
    <span class="hl opt">{</span>   Console<span class="hl opt">.</span><span class="hl kwd">WriteLine</span><span class="hl opt">(</span>message<span class="hl opt">);</span>
        Console<span class="hl opt">.</span><span class="hl kwd">WriteLine</span><span class="hl opt">(</span><span class="hl str">&quot;The program has terminated!&quot;</span><span class="hl opt">);</span>
        Environment<span class="hl opt">.</span><span class="hl kwd">Exit</span><span class="hl opt">(</span><span class="hl num">1</span><span class="hl opt">);</span>
    <span class="hl opt">}</span>

    <span class="hl slc">/// Call the AStyleMainUtf16 function in Artistic Style.</span>
    <span class="hl slc">/// An empty string is returned on error.</span>
    <span class="hl kwa">public string</span> <span class="hl kwd">FormatSource</span><span class="hl opt">(</span><span class="hl kwa">string</span> textIn<span class="hl opt">,</span> <span class="hl kwa">string</span> options<span class="hl opt">)</span>
    <span class="hl opt">{</span>   <span class="hl slc">// Return the allocated string</span>
        <span class="hl slc">// Memory space is allocated by AStyleMemAlloc, a callback function</span>
        <span class="hl kwa">string</span> textOut <span class="hl opt">=</span> String<span class="hl opt">.</span>Empty<span class="hl opt">;</span>
        <span class="hl kwa">try</span>
        <span class="hl opt">{</span>   <span class="hl slc">// NOTE: a Unicode string IS returned here.</span>
            IntPtr fpText <span class="hl opt">=</span> <span class="hl kwd">fpAStyleMainUtf16</span><span class="hl opt">(</span>textIn<span class="hl opt">,</span> options<span class="hl opt">,</span>
                                              AStyleError<span class="hl opt">,</span> AStyleMemAlloc<span class="hl opt">);</span>
            <span class="hl kwa">if</span> <span class="hl opt">(</span>fpText <span class="hl opt">!=</span> IntPtr<span class="hl opt">.</span>Zero<span class="hl opt">)</span>
            <span class="hl opt">{</span>   textOut <span class="hl opt">=</span> Marshal<span class="hl opt">.</span><span class="hl kwd">PtrToStringUni</span><span class="hl opt">(</span>fpText<span class="hl opt">);</span>
                Marshal<span class="hl opt">.</span><span class="hl kwd">FreeHGlobal</span><span class="hl opt">(</span>fpText<span class="hl opt">);</span>
            <span class="hl opt">}</span>
        <span class="hl opt">}</span>
        <span class="hl kwa">catch</span> <span class="hl opt">(</span>Exception e<span class="hl opt">)</span>
        <span class="hl opt">{</span>   Console<span class="hl opt">.</span><span class="hl kwd">WriteLine</span><span class="hl opt">(</span>e<span class="hl opt">.</span><span class="hl kwd">ToString</span><span class="hl opt">());</span>
        <span class="hl opt">}</span>
        <span class="hl kwa">return</span> textOut<span class="hl opt">;</span>
    <span class="hl opt">}</span>

    <span class="hl slc">/// Called by constructor to get the shared library name.</span>
    <span class="hl slc">/// This will get any version of the library in the executable&apos;s directory.</span>
    <span class="hl slc">/// Usually a specific version would be obtained, in which case a constant</span>
    <span class="hl slc">/// could be used for the library name.</span>
    <span class="hl kwa">private string</span> <span class="hl kwd">GetLibraryName</span><span class="hl opt">()</span>
    <span class="hl opt">{</span>   <span class="hl slc">// get a library name in the executable&apos;s directory (any platform)</span>
        <span class="hl slc">// the following example will allow different library names for x86 and x64</span>
        <span class="hl slc">//if (IsWindows())</span>
        <span class="hl slc">//    libraryName = IntPtr.Size == 8 ? &quot;AStyle64-2.06.dll&quot; : &quot;AStyle32-2.06.dll&quot;;</span>
        <span class="hl slc">//else</span>
        <span class="hl slc">//    libraryName = IntPtr.Size == 8 ? &quot;libastyle64-2.06.so&quot; : &quot;libastyle32-2.06.so&quot;;</span>
        <span class="hl kwa">string</span> libraryName <span class="hl opt">=</span> <span class="hl kwa">null</span><span class="hl opt">;</span>
        <span class="hl kwa">string</span> appDirectory <span class="hl opt">=</span> System<span class="hl opt">.</span>AppDomain<span class="hl opt">.</span>CurrentDomain<span class="hl opt">.</span>BaseDirectory<span class="hl opt">;</span>
        <span class="hl kwa">string</span><span class="hl opt">[]</span> files <span class="hl opt">=</span> Directory<span class="hl opt">.</span><span class="hl kwd">GetFiles</span><span class="hl opt">(</span>appDirectory<span class="hl opt">,</span> <span class="hl str">&quot;*.*&quot;</span><span class="hl opt">);</span>
        <span class="hl kwa">foreach</span> <span class="hl opt">(</span><span class="hl kwa">string</span> filePath <span class="hl kwa">in</span> files<span class="hl opt">)</span>
        <span class="hl opt">{</span>   <span class="hl kwa">string</span> fileName <span class="hl opt">=</span> Path<span class="hl opt">.</span><span class="hl kwd">GetFileName</span><span class="hl opt">(</span>filePath<span class="hl opt">).</span><span class="hl kwd">ToLower</span><span class="hl opt">();</span>
            <span class="hl kwa">if</span> <span class="hl opt">((</span>fileName<span class="hl opt">.</span><span class="hl kwd">EndsWith</span><span class="hl opt">(</span><span class="hl str">&quot;.dll&quot;</span><span class="hl opt">)</span>
                    <span class="hl opt">||</span> fileName<span class="hl opt">.</span><span class="hl kwd">Contains</span><span class="hl opt">(</span><span class="hl str">&quot;.so&quot;</span><span class="hl opt">)</span>
                    <span class="hl opt">||</span> fileName<span class="hl opt">.</span><span class="hl kwd">EndsWith</span><span class="hl opt">(</span><span class="hl str">&quot;.dylib&quot;</span><span class="hl opt">))</span>
                    <span class="hl opt">&amp;&amp; (</span>fileName<span class="hl opt">.</span><span class="hl kwd">StartsWith</span><span class="hl opt">(</span><span class="hl str">&quot;astyle&quot;</span><span class="hl opt">)</span>
                        <span class="hl opt">||</span> fileName<span class="hl opt">.</span><span class="hl kwd">StartsWith</span><span class="hl opt">(</span><span class="hl str">&quot;libastyle&quot;</span><span class="hl opt">)))</span>
            <span class="hl opt">{</span>   libraryName <span class="hl opt">=</span> Path<span class="hl opt">.</span><span class="hl kwd">GetFileName</span><span class="hl opt">(</span>filePath<span class="hl opt">);</span>
                <span class="hl kwa">break</span><span class="hl opt">;</span>
            <span class="hl opt">}</span>
        <span class="hl opt">}</span>
        <span class="hl kwa">if</span> <span class="hl opt">(</span>libraryName <span class="hl opt">==</span> <span class="hl kwa">null</span><span class="hl opt">)</span>
        <span class="hl opt">{</span>   <span class="hl kwd">Error</span><span class="hl opt">(</span><span class="hl str">&quot;Cannot find astyle native library in &quot;</span> <span class="hl opt">+</span> appDirectory<span class="hl opt">);</span>
        <span class="hl opt">}</span>
        <span class="hl kwa">return</span> libraryName<span class="hl opt">;</span>
    <span class="hl opt">}</span>

    <span class="hl slc">/// Get the Artistic Style version number.</span>
    <span class="hl kwa">public string</span> <span class="hl kwd">GetVersion</span><span class="hl opt">()</span>
    <span class="hl opt">{</span>   <span class="hl kwa">string</span> version <span class="hl opt">=</span> String<span class="hl opt">.</span>Empty<span class="hl opt">;</span>
        <span class="hl kwa">try</span>
        <span class="hl opt">{</span>   <span class="hl slc">// NOTE: a Unicode string is NOT returned here.</span>
            IntPtr fpVersion <span class="hl opt">=</span> <span class="hl kwd">fpAStyleGetVersion</span><span class="hl opt">();</span>
            <span class="hl kwa">if</span> <span class="hl opt">(</span>fpVersion <span class="hl opt">!=</span> IntPtr<span class="hl opt">.</span>Zero<span class="hl opt">)</span>
            <span class="hl opt">{</span>   version <span class="hl opt">=</span> Marshal<span class="hl opt">.</span><span class="hl kwd">PtrToStringAnsi</span><span class="hl opt">(</span>fpVersion<span class="hl opt">);</span>
            <span class="hl opt">}</span>
        <span class="hl opt">}</span>
        <span class="hl kwa">catch</span> <span class="hl opt">(</span>Exception e<span class="hl opt">)</span>
        <span class="hl opt">{</span>   <span class="hl kwd">Error</span><span class="hl opt">(</span>e<span class="hl opt">.</span><span class="hl kwd">ToString</span><span class="hl opt">());</span>
        <span class="hl opt">}</span>
        <span class="hl kwa">return</span> version<span class="hl opt">;</span>
    <span class="hl opt">}</span>

    <span class="hl slc">/// Determine if this is a Linux platform.</span>
    <span class="hl kwa">private bool</span> <span class="hl kwd">IsLinux</span><span class="hl opt">()</span>
    <span class="hl opt">{</span>   <span class="hl kwa">var</span> p <span class="hl opt">= (</span><span class="hl kwa">int</span><span class="hl opt">)</span>Environment<span class="hl opt">.</span>OSVersion<span class="hl opt">.</span>Platform<span class="hl opt">;</span>
        <span class="hl kwa">return</span> <span class="hl opt">(</span>p <span class="hl opt">==</span> <span class="hl num">4</span><span class="hl opt">) || (</span>p <span class="hl opt">==</span> <span class="hl num">6</span><span class="hl opt">) || (</span>p <span class="hl opt">==</span> <span class="hl num">128</span><span class="hl opt">);</span>
    <span class="hl opt">}</span>

    <span class="hl slc">/// Determine if this is a Windows platform.</span>
    <span class="hl kwa">private bool</span> <span class="hl kwd">IsWindows</span><span class="hl opt">()</span>
    <span class="hl opt">{</span>   <span class="hl kwa">var</span> p <span class="hl opt">= (</span><span class="hl kwa">int</span><span class="hl opt">)</span>Environment<span class="hl opt">.</span>OSVersion<span class="hl opt">.</span>Platform<span class="hl opt">;</span>
        <span class="hl kwa">return</span> <span class="hl opt">(</span>p <span class="hl opt">!=</span> <span class="hl num">4</span><span class="hl opt">) &amp;&amp; (</span>p <span class="hl opt">!=</span> <span class="hl num">6</span><span class="hl opt">) &amp;&amp; (</span>p <span class="hl opt">!=</span> <span class="hl num">128</span><span class="hl opt">);</span>
    <span class="hl opt">}</span>

    <span class="hl slc">/// AStyleMainUtf16 callback to allocate memory for the return string.</span>
    <span class="hl kwa">private</span> IntPtr <span class="hl kwd">AStyleMemAlloc</span><span class="hl opt">(</span><span class="hl kwa">int</span> size<span class="hl opt">)</span>
    <span class="hl opt">{</span>   <span class="hl kwa">return</span> Marshal<span class="hl opt">.</span><span class="hl kwd">AllocHGlobal</span><span class="hl opt">(</span>size<span class="hl opt">);</span>
    <span class="hl opt">}</span>

    <span class="hl slc">/// AStyleMainUtf16 callback to display errors from Artistic Style.</span>
    <span class="hl kwa">private void</span> <span class="hl kwd">AStyleError</span><span class="hl opt">(</span><span class="hl kwa">int</span> errorNumber<span class="hl opt">,</span> <span class="hl kwa">string</span> error<span class="hl opt">)</span>
    <span class="hl opt">{</span>   Console<span class="hl opt">.</span><span class="hl kwd">WriteLine</span><span class="hl opt">(</span><span class="hl str">&quot;AStyle error &quot;</span> <span class="hl opt">+</span> errorNumber <span class="hl opt">+</span> <span class="hl str">&quot;</span><span class="hl esc">\n</span><span class="hl str">&quot;</span> <span class="hl opt">+</span> error<span class="hl opt">);</span>
    <span class="hl opt">}</span>

<span class="hl opt">}</span>   <span class="hl slc">// class AStyleInterface</span>

</pre>
    </div>

    <p>
        &nbsp;</p>

    <center style="margin-left: -0.4in;">
        <a href="http://sourceforge.net/projects/astyle">
            <img src="http://sflogo.sourceforge.net/sflogo.php?group_id=2319&type=16" alt="" /></a>
    </center>

    <p>
        &nbsp;</p>
</body>

</html>

